#删除原容器
echo "begin stop and delete old sonarqube......"
docker stop sonarqube-cqj && docker rm sonarqube-cqj
echo "=======================rm over=============================="


# pull mysql images
echo "=======================start pull sonarqube images=============================="
docker pull sonarqube:10.1-community
echo "=======================pull end.=============================="

# check path
sonarqube_conf_path="/home/docker/sonarqube/conf"
if [[ ! -d "$sonarqube_conf_path" ]]; then
    echo "sonarqube_conf_path not exsits,create now."
    mkdir -p ${sonarqube_conf_path}
else
    echo "sonarqube_conf_path exsits,do not create."
fi
sonarqube_data_path="/home/docker/sonarqube/data"
if [[ ! -d "$sonarqube_data_path" ]]; then
    echo "sonarqube_data_path not exsits,create now."
    mkdir -p ${sonarqube_data_path}
else
    echo "sonarqube_data_path exsits,do not create."
fi
sonarqube_extensions_path="/home/docker/sonarqube/extensions"
if [[ ! -d "$sonarqube_extensions_path" ]]; then
    echo "sonarqube_extensions_path not exsits,create now."
    mkdir -p ${sonarqube_extensions_path}
else
    echo "sonarqube_extensions_path exsits,do not create."
fi
sonarqube_logs_path="/home/docker/sonarqube/logs"
if [[ ! -d "$sonarqube_logs_path" ]]; then
    echo "sonarqube_logs_path not exsits,create now."
    mkdir -p ${sonarqube_logs_path}
else
    echo "sonarqube_logs_path exsits,do not create."
fi

chmod -R 777 /home/docker/sonarqube

#build new sonarqube container
echo "=======================start build sonarqube container=============================="
docker run -d \
-p 9100:9000 \
--ip 172.20.1.9 \
--network=bridge-chen \
--link pgsql-cqj \
-v /home/docker/sonarqube/conf:/opt/sonarqube/conf \
-v /home/docker/sonarqube/data:/opt/sonarqube/data \
-v /home/docker/sonarqube/extensions:/opt/sonarqube/extensions \
-v /home/docker/sonarqube/logs:/opt/sonarqube/logs \
-e SONAR_JDBC_URL=jdbc:postgresql://pgsql-cqj:5432/sonar \
-e SONAR_JDBC_USERNAME=postgres \
-e SONAR_JDBC_PASSWORD=Chenjin1314... \
--privileged=true \
--name sonarqube-cqj \
--restart=always \
sonarqube:10.1-community
echo "=======================build end.=============================="
